﻿#include"test917.h"
#include"room.h"
#include<json\rapidjson.h>
#include<json\document.h>
#include"CommonFunction.h"
#include"CardGame.h"
using namespace std;
Scene *test917::createscene()
{
	return test917::create();
}
bool test917::init()
{
	if (!Scene::init())
	{
		return false;
	}
	
	//加载素材
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("GameSence.plist", "GameSence.png");


	CommonFunction::getChineseWithKey("b");

	Size visibleSize = Director::getInstance()->getVisibleSize();
	//游戏背景
	Sprite *background = Sprite::create("Background.png");
	background->setPosition(visibleSize.width / 2, visibleSize.height / 2);
	addChild(background);


	MenuItemFont *beginItem = MenuItemFont::create(CommonFunction::getChineseWithKey("beginGame")->getCString(), CC_CALLBACK_1(test917::beginGame, this));
	//返回
	MenuItemImage *closeItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png");
	closeItem->setCallback(CC_CALLBACK_1(test917::closeGameScene, this));
	closeItem->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
	closeItem->setPosition(Vec2(-visibleSize.width/2, visibleSize.height/2));
	closeItem->setScale(3);

	Menu *menu = Menu::create(closeItem, beginItem, NULL);
	addChild(menu);
	//创建自己
	self_pos = 1;
	for (int i = 1; i <= 4; i++)
	{
		Player *player = new Player();
		player->account = StringUtils::format("player%d", i);
		player->position = i;
		loadPlayerUI(player);

	}

 
	return true;
}
void test917::beginGame(Ref *pSender)
{
	Size visibleSize = Director::getInstance()->getVisibleSize();
	log("游戏开始～～～");
	// 1.洗牌
	int cards[54];
	CardGame *game = new CardGame();
	game->shuffle(cards, 54);

	// 2.发牌
	player_self->initCards();
	player_right->initCards();
	player_front->initCards();
	player_left->initCards();
	Player *currentPlayer; // 记录当前操作的玩家
	int start_pos = 3; // 先发给3号
	for (int i = 0; i < 54; i++) {
		// 获取当前发牌者的真实位置
		char pos_char = getPlayerCharPosition((start_pos + i - 1) % 4 + 1);

		// ♥️3
		if (cards[i] == 41) {
			game->first = pos_char;
		}
		// ♦️10
		if (cards[i] == 22) {
			game->red10_fk = pos_char;
		}
		// ♥️10
		if (cards[i] == 48) {
			game->red10_hx = pos_char;
		}

		switch (pos_char) {
		case 's':
			currentPlayer = player_self;
			break;
		case 'r':
			currentPlayer = player_right;
			break;
		case 'f':
			currentPlayer = player_front;
			break;
		case 'l':
			currentPlayer = player_left;
			break;
		default:
			break;
		}

		currentPlayer->cards[currentPlayer->countCards()] = cards[i];
	}

	player_self->sortCards();

	// 显示自己的牌
	for (int i = 0; i < player_self->countCards(); i++) {

		string file_name = "";

		if (player_self->cards[i] == 52) {
			// 小王
			file_name = "xiaowang.png";
		}
		else if (player_self->cards[i] == 53) {
			// 大王
			file_name = "dawang.png";
		}
		else {
			// 如何得到花色？
			int flower = player_self->cards[i] / 13 + 1;
			// 牌的值？
			int value = player_self->cards[i] % 13 + 1;
			switch (flower) {
			case 1: // ♠️
				file_name = StringUtils::format("hei-%d.png", value);
				break;
			case 2: // ♦️
				file_name = StringUtils::format("fk-%d.png", value);
				break;
			case 3: // ♣️
				file_name = StringUtils::format("mh-%d.png", value);
				break;
			case 4: // ♥️
				file_name = StringUtils::format("hong-%d.png", value);
				break;
			default:
				break;
			}
		}

		Sprite *cardSprite = Sprite::createWithSpriteFrameName(file_name);
		cardSprite->setPosition(Vec2(cardSprite->getBoundingBox().size.width / 2 + i * 60, visibleSize.height / 2));
		addChild(cardSprite);
	}
}

void test917::setRoomId(int roomId)
{
	Label *label = Label::createWithSystemFont(StringUtils::format("room id=%d", roomId+1), "Arial", 40);
	Size visibleSize = Director::getInstance()->getVisibleSize();
	label->setAnchorPoint(Vec2::ANCHOR_TOP_RIGHT);
	label->setPosition(Vec2(visibleSize.width - 30, visibleSize.height - 30));
	label->enableOutline(Color4B(90, 221, 247, 255), 3);
	
	addChild(label);

}
void test917::closeGameScene(Ref *pSender)
{
	Director::getInstance()->replaceScene(room::createscene());
}
////加载对应玩家的UI信息
char test917::getPlayerCharPosition(int pos)
{
	if (self_pos == 1)//当自己坐在1
	{
		if (pos == 2)return'r';//2在右边
		if (pos == 3)return'f';//3在对面
		if (pos == 4)return'l';//4在左边 
	}
	else if (self_pos == 2)//当自己坐在2上
	{
		if (pos == 1)return'l';//1在左边
		if (pos == 3)return'r';//3在右边
		if (pos == 4)return'f';//4在对面
	}
	else if (self_pos == 3)//当自己坐在3上
	{
		if (pos == 1)return'f';//1在对面
		if (pos == 2)return'l';//3在左边
		if (pos == 4)return'r';//4在右边
	}
	else if (self_pos == 4)//当自己坐在4上
	{
		if (pos == 1)return'r';//1在右边
		if (pos == 2)return'f';//3在对面
		if (pos == 3)return'l';//4在左边
	}
	return's';//以上都不满足，则是自己
}
void test917::loadPlayerUI(Player *player)
{
	//将int的椅子号，转化为相对我所在的位置
	char pos_char1 = getPlayerCharPosition(player->position);
	log("%c", pos_char1);

	player->pos_char = pos_char1;

	player->createPlayerNode();
	addChild(player->node);

	switch (player->pos_char)
		{
		case's':
			player_self = player;
			break;
		case'r':
			player_right = player;
			break;
		case'f':
			player_front = player;
			break;
		case'l':
			player_left = player;
			break;
		}
}
